package org.mnswf.services {
	
	import com.dannypatterson.config.Settings;
	import com.dannypatterson.remoting.FaultEvent;
	import com.dannypatterson.remoting.ResultEvent;
	import com.dannypatterson.remoting.ServiceProxy;
	import flash.events.EventDispatcher;
	import mx.managers.CursorManager;
	import org.mnswf.model.Member;
	
	public class LoginService extends EventDispatcher implements IService {
	
		private var service:ServiceProxy;
		private var emailAddress:String;
		private var password:String;
		
		public function LoginService(emailAddress:String, password:String) {
			this.emailAddress = emailAddress;
			this.password = password;
			service = new ServiceProxy(Settings.getInstance().remotingGateway, Settings.getInstance().memberService);
			service.addEventListener(ResultEvent.RESULT, onComplete, false, 0, true);
			service.addEventListener(FaultEvent.FAULT, onFault, false, 0, true);
		}
		
		private function onComplete(event:ResultEvent):void {
			CursorManager.removeBusyCursor();
			var member:Member = new Member();
			member.parseTransferObject(event.result);
			dispatchEvent(new ResultEvent(ResultEvent.RESULT, event.bubbles, event.cancelable, member));
		}
		
		private function onFault(event:FaultEvent):void {
			CursorManager.removeBusyCursor();
			dispatchEvent(new FaultEvent(FaultEvent.FAULT, event.bubbles, event.cancelable, event.fault));
		}
		
		public function execute():void {
			CursorManager.setBusyCursor();
			service.login(emailAddress, password);
		}
		
	}
}